<?php
declare(strict_types=1);

namespace App\Service;

use Hyperf\DbConnection\Db;
use App\Constants\ReturnCode;
use Throwable;
class AccountService 
{
    /**
     * 添加/减少资金
     * 用于 返佣 管理后台增加、减少资金等直接对资金的操作
     *
     * @param integer $user_id
     * @param integer $money
     * @param string $oper_type
     * @return void
     */
    public function addMoney($user_id = 0,$money = 0,$oper_type = '',$nid = '')
    {
        //用户ID不为空 或者资金为0空 不允许操作用户资金
        if(empty($user_id) || empty($money))
        {
            return false;
        }
        //资金小于0判断用户账号可用资金是否足够，如果不足够，直接返回失败
        if($money<0){
            $row_account = Db::table('user_account')->select('usable_amount')->where('user_id',  $user_id)->first();
            if(empty($row_account)){
                return false;
            }else{
                $balance = $row_account['usable_amount']+$money;
                if($balance<0){
                    return false;
                }
            }
        }
        //开启事务
        Db::beginTransaction();
        try{
            //针对资金账户行锁避免并发情况问题
            $row_account = Db::table('user_account')->where('user_id',  $user_id)->lockForUpdate()->first();
            $old_total_amount = !empty($row_account)?$row_account['total_amount']:0;
            $old_usable_amount = !empty($row_account)?$row_account['usable_amount']:0;
            //没有资金账户的情况下自动创建资金账户/更新资金
            Db::table('user_account')->updateOrInsert(
                ['user_id' =>$user_id],
                ['total_amount' =>bcadd((string)$old_total_amount,(string)$money,2),'usable_amount'=>bcadd((string)$old_usable_amount,(string)$money,2)]
            );
            //添加资金流水
            $data_log = [
                'user_id'=>$user_id,          
                'oper_type'=>$oper_type,          
                'money'=>$money,          
                'nid'=>$nid,          
                'reamrk'=>$this->getRemarkByOperType($oper_type),        
                'created_at'=>time(),        
            ];
            Db::table('user_account_log')->insert($data_log);
            Db::commit();
            return true;
        } catch(Throwable $e){
            Db::rollBack();
            return false;
        }
        //结束事务
    }
    /**
     * 通过操作类型获取资金流水备注
     *
     * @param string $oper_type
     * 
     * @return void
     */
    public function getRemarkByOperType($oper_type=''){
        $ret = '';
        switch ($oper_type) {
            case 'rebate':
                $ret = '直邀返佣';
                break;
            case 'up_one_rebate':
                $ret = '一级代理返佣';
                break;
            case 'up_two_rebate':
                $ret = '二级代理返佣';
                break;
            case 'sys_cut':
                $ret = '系统抽成';
                break;
            default:
                $ret = '未知';
        }
        return $ret;
    }
    /**
     * 冻结资金
     *
     * @param integer $user_id
     * @param integer $money
     * @param string $oper_type
     * @return void
     */
    public function freezeMoney($user_id = 0,$money = 0,$oper_type = '')
    {
        if($money<=0){
            return false;
        }
    }
    /**
     * 解冻资金
     *
     * @param integer $user_id
     * @param integer $money
     * @param string $oper_type
     * @return void
     */
    public function unfreezeMoney($user_id = 0,$money = 0,$oper_type = '')
    {
        if($money<=0){
            return false;
        }
    }

}
